/* ----------------------------------------------------------------------------------------------------------------
 * SoundManager
 * ----------------------------------------------------------------------------------------------------------------
 */
package pure.sound
{
	import pure.engine.sound.MusicSound;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;

	/**
	 * 声音管理器
	 */
public class SoundManager extends EventDispatcher
{
	public function SoundManager() {
		this._musicGroup = { };
		this._effectGroup = { };
		//this.addEventListener(SoundEvent.PLAY_FINISHED, ____onPlayFinished);
	}

	
	
	private var _volumeRatio:Number;    // 音量比率
	
	private var _currMusic:String;					/*当前音乐*/
	private var _musicGroup:Object;				/*名字:背景类声音*/
	private var _effectGroup:Object;				/*名字:效果类声音*/
	
	/**
	 * 添加一个声音
	 * @name				声音名字
	 * @group				声音组(缺省时添加到默认)
	 * @isPreload			是否预加载
	 */	
	/*public static function play(name:String,url:String,type:int,isPreload:Boolean=false):void
	{
		var soundItem:SoundItem=null;
		switch(type){
			case SoundType.MUSIC:
				if(this._musicGroup[name]!=undefined){
					throw new Error("<SoundManager::addSound()::Music>名称重复!");
				}
				soundItem=new MusicSound();
				this._musicGroup[name]=soundItem;
				break;
			case SoundType.EFFECT:
				if(this._effectGroup[name]!=undefined){
					throw new Error("<SoundManager::addSound()::Effect>名称重复!");
				}
				soundItem=new EffectSound();
				this._effectGroup[name]=soundItem;
				break;
			default:
				throw new Error("<SoundManager::addSound()>声音类型错误");
				break;
		}
		soundItem.initialize(url,isPreload);
	}*/

	/**
	 * 播放音乐
	 * <TODO>同时仅放一首
	 * @param sound
	 * @param loop
	 * @param autoDestroy
	 */	
	/*public function playMusic(sound:* , loop:Boolean = true, volume:Number = 1, autoDestroy:Boolean = false ): void {
		if(this._musicGroup[name]==undefined){
			throw new Error("<SoundManager::playMusic()::"+name+">声音尚未添加");
		}
		stopMusic();
		var sound:MusicSound=this._musicGroup[name] as MusicSound;
		sound.play(loop);
		this._currMusic = name;
	}*/
	
	/**
	 * 播放效果声音
	 * <TODO>可重叠播放
	 * @name					声音名字
	 * @loop					循环次数
	 */	
	/*public function playEffect(sound:* , loop:Boolean = true, volume:Number = 1, autoDestroy:Boolean = false):void
	{
		if(this._effectGroup[name]==undefined){
			throw new Error("<SoundManager::playEffect()>声音尚未添加");
		}
		var sound:EffectSound=this._musicGroup[name] as EffectSound;
		sound.play(loop);
	}*/
	
	 /**
	  * 继续
	  */
	public function resumeMusic():void
	{
		if (this._currMusic == null) {
			return;
		}
		var sound:MusicSound = this._musicGroup[this._currMusic] as MusicSound;
		if (sound.isPlaying) {
			return;
		}
		sound.doContinue();
	}
	
	 /**
	  * <停止|暂停>播放音乐
	  * @isPaused				是否为暂停
	  */
	public function stopMusic(isPaused:Boolean=false):void
	{
		if (this._currMusic == null) {
			return;
		}
		var sound:MusicSound = this._musicGroup[this._currMusic] as MusicSound;
		if (!sound.isPlaying) {
			return;
		}
		if (isPaused) {
			sound.stop(true);
		}
		else {
			sound.stop();
			this._currMusic = null;
		}
	}
	
	/**
	 * 
	 **/
	public function removeSound(name:String):void
	{
		this._musicGroup[name].clear();
	}
	
	public function dispose():void
	{

	}
	
	public function getSound(name:String):Sound
	{
		return this._musicGroup[name].sound
	}
	
	
	
	/* ----------------------------------------------------    get / set    ----------------------------------------------------*/
	
	
	
	
	/*当前播放音乐*/
	public function get currMusic():String
	{
		return this._currMusic?this._currMusic:"<null>";
	}
	
	public function get musicGroup():Object
	{
		return this._musicGroup;
	}
	
	public function set musicGroup(v:Object):void
	{
		this._musicGroup = v;
	}
	

}
}